package com.huajx.tuopuda.client.udp;

import com.huajx.core.util.MessageKit;
import com.huajx.service.UdpSentRecord;
import com.huajx.util.ByteUtil;
import com.huajx.util.HexUtil;
import com.huajx.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-24 20:33
 */
public class TuopudaClient2 {

    public static String  card(long n) {
//        long l = 16328325;
        long l1 = n / 100000;
        long l2 = n % 100000;
//        System.out.println(l + " " + l1 + " " + " "+ l2);
//        String s1 = HexUtil.decodeHexStr(Long.toHexString(l1));
//        String s2 = HexUtil.decodeHexStr(Long.toHexString(l2));
//        System.out.println(Long.toHexString(l1) + " " + Long.toHexString(l2));

//        System.out.println(Long.valueOf(Long.toHexString(l1)+ Long.toHexString(l2), 16));
//        System.out.println(Long.valueOf("8EBEAB", 16));
        return Long.toHexString(l1) + Long.toHexString(l2);
    }

    public static void main(String[] args) {
//        System.out.println(card(292));
        System.out.println(card(16328325));
        System.out.println(card(13342949));
        System.out.println(card(15039413));
        System.out.println(card(12036321));
//        133 42949
        String s = "4203872213";
        long l = Long.valueOf(s);
        String hex = Long.toHexString(l);
        long id = TuopudaUtil.toCardId8BitFrom10Bit(s);
        System.out.println("--------------");

        System.out.println(hex);

        System.out.println(l & 0xFFFF);
        System.out.println(Long.toHexString(l & 0xFFFF));
        System.out.println(((l >> 16) & 0xFF) * 100000);

        System.out.println("8位 10进制："+id);
        System.out.println("8位 16进制："+card(id));
        System.out.println("10位 16进制："+hex);

        System.out.println("--------------");



//        render("7ef4f38110191012060643520100007904b57be5004c27783593ff000000049d080d");
//        render("7ef4f381101910120606432104000079047540de004c27673503ff000000044c070d");
//        render("7ef4f381101910120606432104000079048526f9004c275b3593ff00000004e1070d");
        render("7e0095811019101206161810010000030036ff6c014c273a8290330000000441050d");
        render("7e00958110191012061624340200000300b64f0a014c270c83903300000004b3040d");
        render("7e00958110191012061625030200000300f6c20c014c2711839033000000043d050d");
        render108d("7e00958d1036ff6c014c27e08190040000000800000000000000000000000044050d");

        System.out.println();
    }

    private static void render108d(String message) {
        System.out.println(message.substring(5*2, 9*2));
    }

    private static void render(String message) {
        TuopudaMessage msg = new TuopudaMessage();
        byte[] bytes = HexUtil.hexToBytes(message);
        msg.setBytes(bytes);
        int direct = ByteUtil.bytesToShort(new byte[]{bytes[3], bytes[4]});
        msg.setDirect(direct);
        new TuopudaDecode().readRunStatus(msg, bytes);
        String s = StrUtil.sub(message, 17 * 2, 21*2);
        System.out.println(s);
        System.out.println(msg);
    }
//    private DatagramSocket client;
//
//
//    private static final Logger logger = LoggerFactory.getLogger(TuopudaClient2.class);
//    private static int maxNo = 1;
//
//    public void start(String ip, String address) {
//        logger.info("IP为{},的控制器已经开始数据传输！", ip);
//        int port = 60000;
//        //建立死循环，不断发送数据
//        Boolean flag = true;
//        while (flag) {
//            int records = sendAndReeive(ip, port, address, 1);
//            System.out.println("查询总数值："+records);
//            for (int i = 2; i <= records; i++) {
//            	int newRecords = sendAndReeive(ip, port, address, i);
//            }
//            UdpSentRecord.dao.sendJson();//发送消息到平台端
//            //清楚Map里的记录
//            UdpSentRecord.map.clear();
//            //清除考勤记录
//            clear(ip, port, address, records);
//            try {
//                Thread.sleep(6000);// 睡眠1000毫秒
//            } catch (InterruptedException e) {
//                flag = false;
//            }
//        }
//    }
//
//    /*public static void main(String[] args){
//         TuopudaClient2 client = new TuopudaClient2();
//         Scanner scanner = new Scanner(System.in);
//         //建立死循环，不断发送数据
//         while(true){
//             String msg = scanner.nextLine();
//             if(StrUtil.isBlank(msg)) {
//                 continue;
//             }
//             if("##".equals(msg)){
//                 break;
//             }
//             //打印响应的数据
//             System.out.println(client.sendAndReceive("192.168.0.3",60000,msg));
//         }
//     }*/
//    /**
//     * 读取指定索引的信息
//     * @param ip
//     * @param port
//     * @param address
//     * @param pos
//     */
//    private void sendAndReeiveRecord(String ip, int port, String address, int pos) {
//    	 //序号
//        String orderNo = MessageKit.fillzero(pos, 8);//BitUtil.bytesToHex(new byte[]{(byte) pos});
//        //功能号
//        String function = "8d10";
//        //包体
//        String body = address + function + orderNo + "00000000000000000000000000000000000000000000";
//        //校验和
//        String check = MessageKit.checkSumChang(body);
//        //命令
//        String command = "7e" + body + check + "0d";
//        String responseMsg = sendAndReceive(ip, port, command);//发送
//        try {
//			TuopudaMessage message = new TuopudaDecode().decode(HexUtil.hexToBytes(responseMsg));
//			UdpSentRecord.dao.createMap(message);
//        } catch (TuopudaException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//    }
//
//    private int sendAndReeive(String ip, int port, String address, int pos) {
//        //序号
//        String orderNo = MessageKit.fillzero(pos, 8);//BitUtil.bytesToHex(new byte[]{(byte) pos});
//        //功能号
//        String function = "8110";
//        //机器版号
////        String plate="0095";
//        //包体
//        String body = address + function + orderNo + "00000000000000000000000000000000000000000000";
//        //校验和
//        String check = MessageKit.checkSumChang(body);
//        //命令
//        String command = "7e" + body + check + "0d";
//        String responseMsg = sendAndReceive(ip, port, command);
//        try {
//        	System.out.println("原始记录"+responseMsg);
//            TuopudaMessage message = new TuopudaDecode().decode(HexUtil.hexToBytes(responseMsg));
//            if (message.getRecords() > 0) {
//            	UdpSentRecord.dao.createMap(message);
//            }
//            return message.getRecords();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return -1;
//    }
//
//    /**
//     * 清楚命令
//     */
//    public void clear(String ip, int port, String sn, int length) {
//        if (length <= 0) {
//            return;
//        }
//        String size = MessageKit.fillzero(length, 8);
//        String body = sn + "8e10" + size + "00000000000000000000000000000000000000000000";
//        String checkSumChang = MessageKit.checkSumChang(body);//校验和
//        String command = "7e" + body + checkSumChang + "0d";
//        String responseMsg = sendAndReceive(ip, port, command);
//        logger.info("IP为{},的控制器,清除数据！", ip);
//        //System.out.println("清除====="+responseMsg);
//    }
//
//    private String sendAndReceive(String ip, int port, String msg) {
//        String responseMsg = "";
//
//        try {
//            //创建客户端的DatagramSocket对象，不必传入地址和对象
//            client = new DatagramSocket();
//            byte[] sendBytes = HexUtil.hexToBytes(msg);
//            //封装要发送目标的地址
//            InetAddress address = InetAddress.getByName(ip);
//            //封装要发送的DatagramPacket的对象，由于要发送到目的主机，所以要加上地址和端口号
//            DatagramPacket sendPacket = new DatagramPacket(sendBytes, sendBytes.length, address, port);
//
//            try {
//                //发送数据
//                client.send(sendPacket);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            byte[] responseBytes = new byte[34];
//            //创建响应信息的DatagramPacket对象
//            DatagramPacket responsePacket = new DatagramPacket(responseBytes, responseBytes.length);
//            try {
//                //等待响应信息，同服务端一样，客户端也会在这一步阻塞，直到收到一个数据包
//                client.receive(responsePacket);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            //解析数据包内容
//            responseMsg = HexUtil.bytesToHex(responsePacket.getData());//new String(, 0, responsePacket.getLength());
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            //关闭客户端
//            if (client != null) {
//                client.close();
//                client = null;
//            }
//        }
//
//        return responseMsg;
//
//    }
}
